const generateFile = require('../utils/generateFile ');

const renderNpmInitFile = (conf) => {
    console.log('--cur render package.json config --', JSON.stringify(conf));
    return new Promise(async (resolve, reject) => {
        const tempFileContent = createContentTemp(conf);
        const { code } = await generateFile(`dist/package.json`, tempFileContent);
        if(code === 1) resolve({ code: 1, data: 'render package.json successfully !'})
        else reject({ code : -1, data: 'render package.json error !'});
    })
}

const createContentTemp = (conf) => {
    if(!conf) return new Error('no conf input createContentTemp error !') ;
    const { name, version = '1.0.0', description, author } = conf;
    let data =  `{
        "name": "${name}",
        "version": "${version}",
        "description": "${description}",
        "main": "index.js",
        "scripts": {
          "push": "git push origin master",
          "start": "nodemon server.js"
        },
        "author": "${author}",
        "dependencies": {
          "body-parser": "1.19.0",
          "express": "4.17.1",
          "log4js": "3.0.6",
          "multer": "1.4.1",
          "mysql2": "1.6.4",
          "sequelize": "4.41.2",
          "nodemon": "2.0.4"
        }
    }`
    return data
}

module.exports = renderNpmInitFile